package com.asia5b.a5bmall.same.dialog

import android.content.Context
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import com.asia5b.a5bmall.same.R
import com.asia5b.a5mall.middleware.config.UpdateIntentService
import com.asia5b.a5mall.middleware.event.DownloadApkEvent
import com.asia5b.a5mall.middleware.model.other.UpdateModel
import com.asia5b.a5mall.middleware.base.BaseDialog
import com.asia5b.a5mall.middleware.utils.AppUtil
import com.asia5b.a5mall.middleware.utils.LocalManageUtil
import com.asia5b.a5mall.middleware.utils.getEventBus
import kotlinx.android.synthetic.main.dialog_update.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * Created by zhangyuncai on 2020/6/6.
 */
class UpdateDialog constructor(var updateModel: UpdateModel, context: Context) : BaseDialog(context) {

    companion object{
        fun update(context: Context,updateModel: UpdateModel)
        {
            val netVersionCoe = updateModel.build.toLong()
            val localVersionCode = AppUtil.getVersionCode()
            if ((netVersionCoe > localVersionCode) && !TextUtils.isEmpty(updateModel.update_url) && !TextUtils.isEmpty(updateModel.build)) {
                UpdateDialog(updateModel, context).show()
            }
        }
    }

    override val layoutId: Int=R.layout.dialog_update
    override val widthPercent: Double=0.8
    override val gravity: Int=Gravity.CENTER

    override fun initView() {
        getEventBus().register(this)
        //更新日志
        if (updateModel.changelog != null) {
            when (LocalManageUtil.getAppLanguage()) {
                LocalManageUtil.LANGUAGE_CN -> {
                    if (updateModel.changelog!!.containsKey("cnChangelog")) {
                        tv_update_content.setText("${updateModel.changelog!!.get("cnChangelog")}")
                    }
                }
                else -> {//默认英文日志
                    if (updateModel.changelog!!.containsKey("enChangelog")) {
                        tv_update_content.setText("${updateModel.changelog!!.get("enChangelog")}")
                    }
                }
            }
        }
        setCanceledOnTouchOutside(false)
        //是否强制更新
        if (updateModel.force_update == 1) {//强制升级
            //强制升级
            setCancelable(false)
            view_update_middleline.visibility = View.GONE
            tv_update_cancel.visibility = View.GONE
        } else {
            tv_update_cancel.setOnClickListener { v: View? -> dismiss() }
        }
        tv_update_sure.setOnClickListener { v: View? ->//开始安装
            if (updateModel.force_update != 1) {
                dismiss()
            }
            UpdateIntentService.startUpdateIntentService(context, updateModel.update_url, updateModel.build)
        }
    }

    override fun initEvent() {
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun onDownloadApkEvent(event: DownloadApkEvent) {
        pb_update.setProgress(event.progress)
    }

    override fun dismiss() {
        getEventBus().unregister(this)
        super.dismiss()
    }
}